# -*- coding: utf-8 -*-
"""
Created on Thu May 30 22:01:32 2019

@author: HP
"""

import pandas as pd
import numpy as np
from collections import Counter
from featureExtraction import get_all_features
import time

friends_relation = pd.read_csv('../data/true_friends.csv')
print('friends_relation shape:',friends_relation.shape)
user_base = pd.read_csv('../data/slct_users_10att.csv')
print('user_base shape:',user_base.shape)


# =============================================================================
# #获取有30个以上好友的用户，作为实验的new users
# users_with_many_friends = []
# users_with_friends = list(friends_relation.id1)
# c = Counter(users_with_friends)
# for id in c:
#     if c[id]>30:
#         users_with_many_friends.append(id)
# print('Num of friends with 30+ friends: ',len(users_with_many_friends))
# 
# # 保存新用户列表
# uwmf = pd.DataFrame(users_with_many_friends,columns=['id'])[:100]
# uwmf.to_csv('data/new_users.csv',header=True,index=False)
# =============================================================================


# =============================================================================
# #生成candidates_set，共10组，每组1万
# all_users =  np.array(user_base.user_id)
# for i in range(10):
#     random_indexs = np.random.randint(0,len(all_users),10000)
#     candidates1w = all_users[random_indexs]
#     pd.DataFrame(candidates1w,columns=['id']).to_csv('data/candidates1w_%s.csv'%i,index=False)
# =============================================================================


# 开始抽取new users和candidates之间的特征了！100×10000
candidates_No = 1
print('candidates_No:::::',candidates_No)
new_users = pd.read_csv('../data/users_with_100_friends.csv')[:100]
for i,new_id in enumerate(list(new_users.id)):
    print('Now is extracting features for %dth new user...'%i)
    # 构建当前new user和1w个candidates的user pairs
    id1s = pd.DataFrame([new_id]*10000)
    id2s = pd.read_csv('../data/candidates1w_%d.csv'%candidates_No)
    user_pairs = pd.concat([id1s,id2s],axis=1)
    user_pairs.columns = ['id1','id2']
    
    t1 = time.time()
    features_df = get_all_features(user_base,friends_relation,user_pairs)
    t2 = time.time()
    print('10000-features time cost:',(t2-t1)/60,'minutes')
    
    features_df.to_csv('../data/extracted_features/Candidates_%d-NewId_%d.csv'%(candidates_No,new_id))
print('candidates_No:::::',candidates_No)











